package top.datacluster.basic.algorithm.leetcode;

import java.util.Stack;

/**
 *
 */
public class ValidBracketLeetcode20 {

    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++){
            if (s.charAt(i) == '(' || s.charAt(i) == '[' || s.charAt(i) == '{'){
                stack.push(s.charAt(i));

            }else{
                if (stack.isEmpty()){
                    return false;
                }
                char c = stack.pop();
                if (c == '(' && s.charAt(i) != ')' || c == '[' && s.charAt(i) != ']' || c == '{' && s.charAt(i) != '}')
                    return false;

            }
        }
        if (stack.isEmpty()){
            return true;
        }else {
            return false;
        }
    }

    public static void main(String[] args) {
        System.out.println(new ValidBracketLeetcode20().isValid("()[]{}"));
    }
}
